(function ($) {
    "use strict";
     //自定义插件
    let EdenPlugin = (function () {
        function EdenPlugin(element, options) {
            this.settings = $.extend(true, $.fn.EdenScroller.defaults, options || {});
            this.element = element;
            if(this.settings.loadMoreEle===""){
                this.settings.loadMoreEle=$("<div class='loadMoreEle'>loading</div>");
            }else {
                this.settings.loadMoreEle=$(this.settings.loadMoreEle);
                this.settings.loadMoreEle.addClass("loadMoreEle");
            }
            this.loading=false;
            this._initEvent();
        }

        EdenPlugin.prototype = {

            _initEvent: function () {
                let me = this;
                var vHeight = document.body.clientHeight || document.documentElement.clientHeight;
                $(window).on("scroll", function (e) {
                    let scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
                    var contentHeight =me.element[0].scrollHeight;
                    if (vHeight + scrollTop >= contentHeight&&!me.loading) {
                         me.showLoadMoreEle();
                        if (me.settings.callback && $.type(me.settings.callback) === "function") {
                            me.settings.callback(me);
                        }
                    }
                });
            },
            showLoadMoreEle:function () {
                this.loading=true;
                this.element.append(this.settings.loadMoreEle);
            },
            hideLoadMoreEle:function () {
                this.loading=false;
                $(".loadMoreEle").remove();
            }

        };
        return EdenPlugin;
    })();

    $.fn.EdenScroller = function (options) {
        if(this.length===0){//表示selector没有对应的元素
            return;
        }
        return this.each(function (index) {
            let $this = $(this), instance = $this.data("edenPlugin");
            if (!instance) {
                instance = new EdenPlugin($this, options)
                $this.data("edenPlugin", instance)
            }
        })
    };
    $.fn.EdenScroller.defaults = {
        offsetPosition: 50,
        callback:"",
        loadMoreEle:""
    };
    $(function () {
        $("[EdenScroller]").EdenScroller()
    });
})(jQuery);